package com.example.common.core.response;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.LinkedHashMap;

/**
 * @author chenjf
 * @Date 2022/9/19
 * @Description
 */
public class AjaxResult extends LinkedHashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";

    /**
     * 返回内容
     */
    public static final String MSG_TAG = "message";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    public static final String ERROR_TAG = "error";

    /**
     * 初始化一个新创建的 AjaxResult 对象，使其表示一个空消息。
     */
    public AjaxResult() {
    }

    public AjaxResult(int code, Object data, String message) {
        super.put(CODE_TAG, code);
        if (ObjectUtil.isNotEmpty(data)) {
            super.put(DATA_TAG, data);
        }
        super.put(MSG_TAG, message);
    }


    public AjaxResult(int code, Object data, String message, String error) {
        super.put(CODE_TAG, code);
        if (ObjectUtil.isNotEmpty(data)) {
            super.put(DATA_TAG, data);
        }
        super.put(MSG_TAG, message);
        super.put(ERROR_TAG, error);
    }

    /* 操作成功 */
    public static AjaxResult success() {
        return new AjaxResult(HttpStatus.OK.value(), null, HttpStatus.OK.getReasonPhrase());
    }

    /* 操作成功 */
    public static AjaxResult success(Object data) {
        return new AjaxResult(HttpStatus.OK.value(), data, HttpStatus.OK.getReasonPhrase());
    }

    /* 操作成功 */
    public static AjaxResult success(Object data, String message) {
        return new AjaxResult(HttpStatus.OK.value(), data, message);
    }

    /* 操作失败 */
    public static AjaxResult error() {
        return new AjaxResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    /* 操作失败 */
    public static AjaxResult error(String message) {
        return new AjaxResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, message);
    }

    /* 自定义返回状态码 */
    public static AjaxResult warn(HttpStatus code, Object data, String message) {

        return new AjaxResult(code.value(), data, message);
    }



}
